package com.xckj.teacher.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;

import com.xckj.teacher.R;

/**
 * @Author: ChenYao
 * @Describe: 更新版本的Dialog
 * @Date: 2024/6/17 20:46
 */
public class UpdateVersionDialog {
    private final Context context;
    private final int themeResId;
    private final View layout;
    private CharSequence versionName, updateContent, notUpdate, update;
    private View.OnClickListener notUpdateClickListener, updateClickListener;
    private boolean isForceUpdate = false;

    public UpdateVersionDialog(Context context) {
        this(context, R.style.CustomDialogStyle);
    }

    @SuppressLint("InflateParams")
    public UpdateVersionDialog(Context context, int themeResId) {
        this(context, themeResId, ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_update_version, null));
    }

    /**
     * 自定义layout用这个
     */
    public UpdateVersionDialog(Context context, int themeResId, View layout) {
        this.context = context;
        this.themeResId = themeResId;
        this.layout = layout;
    }

    public UpdateVersionDialog setVersionName(CharSequence versionName) {
        this.versionName = versionName;
        return this;
    }

    public UpdateVersionDialog setUpdateContent(CharSequence updateContent) {
        this.updateContent = updateContent;
        return this;
    }

    public UpdateVersionDialog setNotUpdateText(CharSequence str) {
        this.notUpdate = str;
        return this;
    }

    public UpdateVersionDialog setUpdateText(CharSequence str) {
        this.update = str;
        return this;
    }

    public UpdateVersionDialog setUpdateOnClickListener(View.OnClickListener listener) {
        this.updateClickListener = listener;
        return this;
    }

    public UpdateVersionDialog setNotUpdateOnClickListener(View.OnClickListener listener) {
        this.notUpdateClickListener = listener;
        return this;
    }

    public Dialog build() {
        final Dialog dialog = new Dialog(context, themeResId);
        dialog.setCancelable(false);
        dialog.addContentView(layout, new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));
        /*设置显不显示*/
        setText(versionName, R.id.tv_version_name);
        setText(updateContent, R.id.tv_update_content);
        setText(notUpdate, R.id.tv_not_update);
        setText(update, R.id.tv_update);
        if (isForceUpdate) {
            layout.findViewById(R.id.tv_not_update).setVisibility(View.GONE);
        } else {
            layout.findViewById(R.id.tv_not_update).setVisibility(View.VISIBLE);
        }
        /*设置点击监听*/
        if (updateClickListener != null) {
            layout.findViewById(R.id.tv_update).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    updateClickListener.onClick(view);
                    dialog.dismiss();
                }
            });
        }
        if (notUpdateClickListener != null) {
            layout.findViewById(R.id.tv_not_update).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    notUpdateClickListener.onClick(view);
                    dialog.dismiss();
                }
            });
        }
        /*设置宽度*/
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (int) (context.getResources().getDisplayMetrics().widthPixels * 0.80);
        dialog.getWindow().setAttributes(params);
        return dialog;
    }

    private void setText(CharSequence text, int id) {
        if (isValid(text)) {
            TextView textView = (TextView) layout.findViewById(id);
            textView.setText(text);
            textView.setVisibility(View.VISIBLE);
        }
    }

    private boolean isValid(CharSequence text) {
        return text != null && !"".equals(text.toString().trim());
    }

    public UpdateVersionDialog isForceUpdate(boolean isForceUpdate) {
        this.isForceUpdate = isForceUpdate;
        return this;
    }
}


